package frame;

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

public class MyMainFrame extends JFrame implements ActionListener,
				InternalFrameListener {

	CustomDesktop desktop;
	private JLabel statusText = new JLabel("Ready");
	private int openFrameCount = 0;
	private MyInternalFrame aInternalFrame;
	private Container containMainFrame;
	private JInternalFrame[] internalFrameAll;

	public MyMainFrame() {
		super("Database Evolution Editor VII");
		// Make the big window be indented 50 pixels from each edge
		// of the screen.
		int inset = 50;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height
				- inset * 2);

		// set up desktop,Menubar,Statusbar GUI
		setupGUI();
	}

	// React to menu selections.
	public void actionPerformed(ActionEvent e) {

		if ("new".equals(e.getActionCommand())) { // new
			createInternalFrame();
		} else if ("test db".equals(e.getActionCommand())) {
			testDatabaseConnection();
		} else if ("create line".equals(e.getActionCommand())) {

		} else { // quitw
			quit();
		}
	}

	// event When InternalFrame Move
	ComponentListener myComponentListener = new ComponentAdapter() {
		@Override
		public void componentMoved(ComponentEvent e) {
			desktop.repaint();
		}
	};

	// event of internalframe2
	public void internalFrameOpened(InternalFrameEvent e) {

	}

	public void internalFrameClosing(InternalFrameEvent e) {

	}

	public void internalFrameClosed(InternalFrameEvent e) {

	}

	public void internalFrameIconified(InternalFrameEvent e) {

	}

	public void internalFrameDeiconified(InternalFrameEvent e) {

	}

	public void internalFrameActivated(InternalFrameEvent e) {

		isSelectedInternalframe();

	}

	public void internalFrameDeactivated(InternalFrameEvent e) {

	}

	public String isSelectedInternalframe() {

		String nameOfInternalFrame = null;
		internalFrameAll = desktop.getAllFrames();

		for (int i = 0; i < internalFrameAll.length; i++) {
			if (internalFrameAll[i].isSelected()) {
				nameOfInternalFrame = internalFrameAll[i].getName();
				Point p = internalFrameAll[i].getLocation();
				statusText.setText("SELECT : " + nameOfInternalFrame
						+ "   POSITTION :( +" + (int) p.getX() + ","
						+ (int) p.getY() + " )");
			}
		}
		return nameOfInternalFrame;
	}

	// Test a connection Database
	protected void testDatabaseConnection() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url = "jdbc:oracle:thin:@//192.168.1.2:1521/homelek";
			try {
				Connection conn = DriverManager.getConnection(url, "system",
						"123456");
				conn.setAutoCommit(false);
			} catch (SQLException ex) {
				statusText.setText("Username or password incorrect.");
			}
		} catch (ClassNotFoundException ex) {
			statusText.setText("Driver or url error!");
		}
		statusText.setText("Test Database Connection Success!");
	}

	// Quit the application.
	protected void quit() {
		System.exit(0);
	}

	// Create GUI ALL
	protected void setupGUI() {
		// Set up the GUI.
		
		desktop = new CustomDesktop(); // a specialized layered pane
		createInternalFrame(); // create first "window"
		createInternalFrame(); // create first "window"
		setContentPane(desktop);
		setJMenuBar(createMenuBar());
		createStatusBar();

		// Make dragging a little faster but perhaps uglier.
		desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
	}

	protected JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();

		// Set up the lone menu.
		JMenu documentMenu = new JMenu("Document");
		documentMenu.setMnemonic(KeyEvent.VK_D);
		menuBar.add(documentMenu);

		JMenu connectionMenu = new JMenu("Connection");
		connectionMenu.setMnemonic(KeyEvent.VK_C);
		menuBar.add(connectionMenu);

		// Set up the first document item.
		JMenuItem documentItem = new JMenuItem("New");
		documentItem.setMnemonic(KeyEvent.VK_N);
		documentItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
				ActionEvent.ALT_MASK));
		documentItem.setActionCommand("new");
		documentItem.addActionListener(this);
		documentMenu.add(documentItem);

		// Set up the second document item.
		documentItem = new JMenuItem("Quit");
		documentItem.setMnemonic(KeyEvent.VK_Q);
		documentItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
				ActionEvent.ALT_MASK));
		documentItem.setActionCommand("quit");
		documentItem.addActionListener(this);
		documentMenu.add(documentItem);

		// set up first connect item
		JMenuItem connectionItem = new JMenuItem("Test DB");
		connectionItem.setMnemonic(KeyEvent.VK_T);
		connectionItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
				ActionEvent.ALT_MASK));
		connectionItem.setActionCommand("test db");
		connectionItem.addActionListener(this);
		connectionMenu.add(connectionItem);
		
		// set up second connect item
		JMenuItem createlinkItem = new JMenuItem("create line");
		createlinkItem.setMnemonic(KeyEvent.VK_L);
		createlinkItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,
				ActionEvent.ALT_MASK));
		createlinkItem.setActionCommand("create line");
		createlinkItem.addActionListener(this);
		connectionMenu.add(createlinkItem);

		return menuBar;
	}

	protected void createStatusBar() {
		// set up Status Bar
		containMainFrame = getContentPane();
		containMainFrame.setLayout(new BorderLayout());
		JPanel StatusBar = new JPanel();
		StatusBar.setLayout(new BoxLayout(StatusBar, BoxLayout.Y_AXIS));
		StatusBar.add(new JSeparator());
		StatusBar.add(statusText);
		StatusBar.setBackground(Color.cyan);
		StatusBar.setVisible(true);
		containMainFrame.add(StatusBar, BorderLayout.SOUTH);
	}

	protected void createInternalFrame() {
		aInternalFrame = new MyInternalFrame(++openFrameCount);		
		aInternalFrame.setName("Doc #" + openFrameCount);
		aInternalFrame.addInternalFrameListener(this);
		aInternalFrame.addComponentListener(myComponentListener);
		//aInternalFrame.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
		aInternalFrame.setVisible(true);
		desktop.setLayout(null);
		desktop.add(aInternalFrame);
		try {
			aInternalFrame.setSelected(true);
		} catch (java.beans.PropertyVetoException e) {
		}
	}

	private static void createAndShowGUI_MainWindow() {
		/**
		 * Create the GUI and show it. For thread safety, this method should be
		 * invoked from the event-dispatching thread.
		 */
		// Make sure we have nice window decorations.
		JFrame.setDefaultLookAndFeelDecorated(false);
		

		// Create and set up the window.
		MyMainFrame MainFrame = new MyMainFrame();
		
		// Make  window system decorations.
		MainFrame.setUndecorated(false);
		
		MainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		MainFrame.setVisible(true);
	}

	public static void main(String[] args) {
		
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI_MainWindow();
			}
		});
	}

	class CustomDesktop extends JDesktopPane {

		private LinearGradientPaint gradientPaint;

		public CustomDesktop() {
			super();
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);

			makeGradientPaint();
			((Graphics2D) g).setPaint(gradientPaint);
			g.fillRect(0, 0, desktop.getWidth(), desktop.getHeight());
			g.setColor(Color.BLACK);
			internalFrameAll = desktop.getAllFrames();
			if (internalFrameAll.length > 1) {
				// paint line between frame1 & frame2
				int x_start = (internalFrameAll[0].getX() + internalFrameAll[0]
						.getWidth());
				int y_start = (internalFrameAll[0].getY() + internalFrameAll[0]
						.getHeight() / 2);
				int x_end = (internalFrameAll[1].getX());
				int y_end = (internalFrameAll[1].getY() + internalFrameAll[1]
						.getHeight() / 2);

				g.drawLine(x_start, y_start, x_end, y_end);
			} else {
				g.setColor(Color.WHITE);
			}
		}

		protected void makeGradientPaint() {
			Point2D start = new Point2D.Float(0, 0);
			Point2D end = new Point2D.Float(getWidth(), 0);
			float[] dist = { 0.0f, 1.0f };
			Color[] colors = { Color.WHITE, Color.WHITE };
			gradientPaint = new LinearGradientPaint(start, end, dist, colors,
					java.awt.MultipleGradientPaint.CycleMethod.NO_CYCLE);
		}
	}

}
